package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import fr.bordeaux1.planingdesetudiants.model.Etudiant;
import fr.bordeaux1.planingdesetudiants.model.Ue;

public class CompositeEtu extends Composite implements IOnglet {
	private IFenetrePrincipale fenetrePrincipale;
	private List listeEtu;
	private Text textNoEtu;
	private Text textName;
	private Text textSurname;
	private Table checkBoxGroup;
	private Text textFiltre;
	private Button btnSupprimer;
	private Group grpAjouterUntudiant;
	private java.util.List<Etudiant> etudiants, etudiantsAffiches;

	public CompositeEtu(Composite arg0, int arg1,
			IFenetrePrincipale fenetrePrincipale) {
		super(arg0, SWT.NONE);

		this.fenetrePrincipale = fenetrePrincipale;
		setLayout(new FormLayout());

		textFiltre = new Text(this, SWT.BORDER);
		textFiltre.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				refreshData();
			}
		});
		FormData fd_textFiltre = new FormData();
		fd_textFiltre.right = new FormAttachment(0, 158);
		fd_textFiltre.top = new FormAttachment(0, 5);
		fd_textFiltre.left = new FormAttachment(0, 5);
		textFiltre.setLayoutData(fd_textFiltre);

		listeEtu = new List(this, SWT.V_SCROLL | SWT.BORDER | SWT.SINGLE);
		FormData fd_listeEtu = new FormData();
		fd_listeEtu.top = new FormAttachment(textFiltre, 5);
		fd_listeEtu.right = new FormAttachment(0, 158);
		fd_listeEtu.left = new FormAttachment(0, 5);
		listeEtu.setLayoutData(fd_listeEtu);

		listeEtu.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				// System.out.println(listeEtu.getVerticalBar().getSelection());
				System.out.println("listeEtu : " + listeEtu.getSelectionIndex());
				if (listeEtu.getSelectionIndex() >= 0)
					etudiantSelectionne();
				else
					aucuneSelection();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				System.out.println("default selected : "
						+ listeEtu.getSelectionIndex());
			}
		});

		Label choixUe = new Label(this, SWT.NONE);
		FormData fd_choixUe = new FormData();
		fd_choixUe.bottom = new FormAttachment(0, 51);
		fd_choixUe.right = new FormAttachment(0, 445);
		fd_choixUe.top = new FormAttachment(0, 51);
		fd_choixUe.left = new FormAttachment(0, 163);
		choixUe.setLayoutData(fd_choixUe);
		choixUe.setText("Selectionnez les UEs de l'étudiant");

		checkBoxGroup = new Table(this, SWT.CHECK);
		checkBoxGroup.setLinesVisible(true);
		FormData fd_checkBoxGroup = new FormData();
		fd_checkBoxGroup.top = new FormAttachment(textFiltre, 5, SWT.TOP);
		fd_checkBoxGroup.left = new FormAttachment(listeEtu, 6);
		fd_checkBoxGroup.right = new FormAttachment(100, -5);
		checkBoxGroup.setLayoutData(fd_checkBoxGroup);
		checkBoxGroup.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (arg0.detail == SWT.CHECK) {
					TableItem item = (TableItem) arg0.item;
					Etudiant etu = etudiantsAffiches.get(listeEtu
							.getSelectionIndex());
					Ue ue = (Ue) item.getData();
					try {
						if (item.getChecked()) {
							CompositeEtu.this.fenetrePrincipale.getDb()
									.ajouterEtudiantAUe(ue, etu);
						} else {
							CompositeEtu.this.fenetrePrincipale.getDb()
									.retirerEtudiantUe(ue, etu);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
					//CompositeEtu.this.fenetrePrincipale.modificationApportees();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
		try {
			java.util.List<Ue> res = fenetrePrincipale.getDb().listerUes();

			for (Ue ue : res) {
				TableItem item = new TableItem(checkBoxGroup, SWT.CHECK
						| SWT.V_SCROLL | SWT.H_SCROLL);
				item.setData(ue);
				item.setText(ue.getCode() + " " + ue.getLibelle());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		btnSupprimer = new Button(this, SWT.NONE);
		btnSupprimer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				int selection = listeEtu.getSelectionIndex();
				Etudiant etu = etudiantsAffiches.get(selection);
				System.out.println("etudiantsAffiches.size()="
						+ etudiantsAffiches.size());
				System.out.println("Supprime " + etu);
				try {
					CompositeEtu.this.fenetrePrincipale.getDb()
							.supprimerEtudiant(etu);
					CompositeEtu.this.fenetrePrincipale.updateStatusBar("Étudiant supprimé avec succès");
					refreshData();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		btnSupprimer.setEnabled(false);
		fd_listeEtu.bottom = new FormAttachment(btnSupprimer, -6);
		FormData fd_btnSupprimer = new FormData();
		fd_btnSupprimer.right = new FormAttachment(textFiltre, 0, SWT.RIGHT);
		fd_btnSupprimer.bottom = new FormAttachment(100, -5);
		fd_btnSupprimer.left = new FormAttachment(0, 5);
		btnSupprimer.setLayoutData(fd_btnSupprimer);
		btnSupprimer.setText("Supprimer");

		grpAjouterUntudiant = new Group(this, SWT.NONE);
		fd_checkBoxGroup.bottom = new FormAttachment(grpAjouterUntudiant, -6);
		grpAjouterUntudiant.setLayout(new FormLayout());
		FormData fd_grpAjouterUntudiant = new FormData();
		fd_grpAjouterUntudiant.top = new FormAttachment(100, -136);
		fd_grpAjouterUntudiant.bottom = new FormAttachment(100, -5);
		fd_grpAjouterUntudiant.right = new FormAttachment(100, -5);
		fd_grpAjouterUntudiant.left = new FormAttachment(0, 163);
		grpAjouterUntudiant.setLayoutData(fd_grpAjouterUntudiant);
		grpAjouterUntudiant.setText("Ajouter un étudiant");

		Label noEtu = new Label(grpAjouterUntudiant, SWT.NONE);
		FormData fd_noEtu = new FormData();
		fd_noEtu.top = new FormAttachment(0, 8);
		fd_noEtu.left = new FormAttachment(0, 5);
		noEtu.setLayoutData(fd_noEtu);
		noEtu.setText("N° Etudiant :");

		textNoEtu = new Text(grpAjouterUntudiant, SWT.BORDER);
		FormData fd_textNoEtu = new FormData();
		fd_textNoEtu.top = new FormAttachment(noEtu, -3, SWT.TOP);
		fd_textNoEtu.left = new FormAttachment(noEtu, 6);
		fd_textNoEtu.right = new FormAttachment(100, -5);
		textNoEtu.setLayoutData(fd_textNoEtu);

		Label nameEtu = new Label(grpAjouterUntudiant, SWT.NONE);
		FormData fd_nameEtu = new FormData();
		fd_nameEtu.top = new FormAttachment(0, 34);
		fd_nameEtu.left = new FormAttachment(0, 5);
		nameEtu.setLayoutData(fd_nameEtu);
		nameEtu.setText("Nom :");

		textName = new Text(grpAjouterUntudiant, SWT.BORDER);
		FormData fd_textName = new FormData();
		fd_textName.right = new FormAttachment(100, -5);
		fd_textName.top = new FormAttachment(0, 31);
		fd_textName.left = new FormAttachment(0, 77);
		textName.setLayoutData(fd_textName);

		Label surnameE = new Label(grpAjouterUntudiant, SWT.NONE);
		FormData fd_surnameE = new FormData();
		fd_surnameE.top = new FormAttachment(0, 60);
		fd_surnameE.left = new FormAttachment(0, 5);
		surnameE.setLayoutData(fd_surnameE);
		surnameE.setText("Prenom :");

		textSurname = new Text(grpAjouterUntudiant, SWT.BORDER);
		FormData fd_textSurname = new FormData();
		fd_textSurname.right = new FormAttachment(100, -5);
		fd_textSurname.top = new FormAttachment(0, 57);
		fd_textSurname.left = new FormAttachment(0, 77);
		textSurname.setLayoutData(fd_textSurname);

		Button addEtu = new Button(grpAjouterUntudiant, SWT.NONE);
		FormData fd_addEtu = new FormData();
		fd_addEtu.right = new FormAttachment(50, 50);
		fd_addEtu.top = new FormAttachment(0, 83);
		fd_addEtu.left = new FormAttachment(50, -50);
		addEtu.setLayoutData(fd_addEtu);
		addEtu.setText("Ajouter");
		addEtu.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				ajouterEtudiant();
				CompositeEtu.this.fenetrePrincipale.updateStatusBar("Étudiant ajouté avec succès");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}
		});
	}

	@Override
	public void refreshData() {
		try {
			etudiants = fenetrePrincipale.getDb().listerEtudiants();
			afficherEtudiantsFiltres();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void afficherEtudiantsFiltres() {
		aucuneSelection();
		listeEtu.removeAll();
		etudiantsAffiches = new ArrayList<Etudiant>();
		for (Etudiant etudiant : etudiants) {
			if (textFiltre.getText().length() == 0
					|| (etudiant.getNom() + " " + etudiant.getPrenom())
							.toLowerCase().contains(
									textFiltre.getText().toLowerCase())
					|| (etudiant.getPrenom() + " " + etudiant.getNom())
							.toLowerCase().contains(
									textFiltre.getText().toLowerCase())
					|| ("" + etudiant.getNumero()).toLowerCase().contains(
							textFiltre.getText().toLowerCase())) {
				listeEtu.add(etudiant.getNom() + " " + etudiant.getPrenom());
				etudiantsAffiches.add(etudiant);
			}
		}
	}

	public void aucuneSelection() {
		btnSupprimer.setEnabled(false);
		checkBoxGroup.setVisible(false);
		listeEtu.setSelection(-1);
	}

	public void etudiantSelectionne() {
		btnSupprimer.setEnabled(true);
		checkBoxGroup.setVisible(true);

		java.util.List<Ue> uesEtu;
		try {
			uesEtu = fenetrePrincipale.getDb().listerUesEtudiant(
					etudiantsAffiches.get(listeEtu.getSelectionIndex()));
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}

		TableItem[] test = checkBoxGroup.getItems();
		for (TableItem tableItem : test) {
			Ue ue = (Ue) tableItem.getData();
			tableItem.setChecked(uesEtu.contains(ue));
		}
	}

	public void ajouterEtudiant() {
		Etudiant etudiant = new Etudiant();
		etudiant.setNom(textName.getText());
		etudiant.setPrenom(textSurname.getText());
		etudiant.setNumero(Integer.parseInt(textNoEtu.getText()));
		try {
			fenetrePrincipale.getDb().ajouterEtudiant(etudiant);
			textName.setText("");
			textSurname.setText("");
			textNoEtu.setText("");
		} catch (SQLException e1) {
			System.out.println("etudiant pas ajouté");
			e1.printStackTrace();
		}
		CompositeEtu.this.fenetrePrincipale.modificationApportees();
	}
}
